package com.cxl.demo.controller;

import com.cxl.demo.mapper.AssigmentMapper;
import com.cxl.demo.service.AssigmentService;
import com.cxl.demo.util.Distance;
import com.cxl.demo.util.TimeUpd;
import com.cxl.demo.vo.Assigment;
import com.cxl.demo.vo.ReturnData;
import com.cxl.demo.vo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/assigment")
@CrossOrigin
public class AssigmentController {

    @Autowired
    ReturnData returnData;
    @Autowired
    AssigmentService assigmentService;

    @Autowired
    AssigmentMapper assigmentMapper;
    @Autowired
    User user;

    @PostMapping("/AddAssigment")
    public ReturnData AddAssigment(@RequestBody Assigment assigment) {
        returnData.setData(null);
        TimeUpd timeUpd = new TimeUpd();
        assigment.setUserId(user.getUserId());

        System.out.println(assigment.getEndTime());
        assigment.setEndTime(timeUpd.UploadDate(assigment.getEndTime()));

        assigment.setStartTime(timeUpd.getDate());
        System.out.println(assigment.getEndTime());
        Integer ok = assigmentService.addAssigment(assigment);
        returnData.setStatus(true);
        returnData.setMsg("创建任务成功");

        return returnData;
    }

    @CrossOrigin
    @PostMapping("/GetAssigmentList")
    public ReturnData GetAssigmentList(@RequestBody Map<String, Object> map) {
        returnData.setData(null);

        map.put("keyword", "%" + (String) map.get("placeName") + "%");

        Integer condition=Integer.parseInt((String)map.get("condition"));

        if(condition==2){
            map.put("assigmentCondition",condition);
        }




        System.out.println(map);
        map.put("page", ((Integer) map.get("currentPage") - 1) * (Integer) map.get("pageSize"));
        map.put("size", (Integer) map.get("pageSize"));


        returnData.setData(assigmentService.getAssigment(map));
        returnData.setMsg("查询完毕");
        returnData.setStatus(true);
        return returnData;

    }

    @PostMapping("/GetMyAssigmentList")
    public ReturnData GetMyAssigmentList(@RequestBody Map<String, Object> map) {
        returnData.setData(null);
        map.put("page", ((Integer) map.get("currentPage") - 1) * (Integer) map.get("pageSize"));
        map.put("size", (Integer) map.get("pageSize"));
        map.put("userId", user.getUserId());
        returnData.setData(assigmentService.getMyAssigment(map));
        returnData.setMsg("查询完毕");
        returnData.setStatus(true);
        return returnData;
    }

    @PostMapping("/GetMyAssigmentById")
    public ReturnData GetMyAssigmentById(@RequestBody Map<String, Object> map) {
        returnData.setData(null);

        returnData.setData(assigmentService.getAssigmentById(Integer.parseInt((String) map.get("assigmentId"))));
        returnData.setMsg("查询完毕");
        returnData.setStatus(true);
        return returnData;
    }


    @PostMapping("/getRootAllEmergency")
    public ReturnData getRootAllEmergency(@RequestBody Map<String, Object> map) {
        returnData.setData(null);

        map.put("page", ((Integer) map.get("currentPage") - 1) * (Integer) map.get("pageSize"));
        map.put("size", (Integer) map.get("pageSize"));


        returnData.setData(assigmentService.getAllEmergency(map));
        returnData.setMsg("查询完毕");
        returnData.setStatus(true);
        return returnData;
    }

    @PostMapping("/UpdateAssigment")
    public ReturnData UpdateAssigment(@RequestBody Assigment assigment) {
        returnData.setData(null);
        Integer ok = assigmentService.updateAssigment(assigment);
        if (ok != 0) {
            returnData.setStatus(true);
            returnData.setMsg("修改任务成功");
        } else {
            returnData.setStatus(false);
            returnData.setMsg("修改任务失败");
        }
        return returnData;
    }
    @PostMapping("/EditAssigment")
    public ReturnData EditAssigment(@RequestBody Assigment assigment) {
        returnData.setData(null);
        Integer ok = assigmentService.EditAssigment(assigment);
        if (ok != 0) {
            returnData.setStatus(true);
            returnData.setMsg("修改任务成功");
        } else {
            returnData.setStatus(false);
            returnData.setMsg("修改任务失败");
        }
        return returnData;
    }



    @PostMapping("/DelAssigment")
    public ReturnData DelAssigment(@RequestBody Map<String, Object> map) {
        returnData.setData(null);

        Integer ok = assigmentService.deleteAssigment((Integer) map.get("assigmentId"));
        if (ok != 0) {
            returnData.setStatus(true);
            returnData.setMsg("删除任务成功");
        } else {
            returnData.setStatus(false);
            returnData.setMsg("删除任务失败,该任务已被接受");
        }
        return returnData;
    }

    @CrossOrigin
    @PostMapping("/GetNearAssigment")
    public ReturnData GetNearAssigment(@RequestBody Map<String, Object> map) {
        returnData.setData(null);

        List<Assigment> list = assigmentMapper.findAll();

        Double Px = (Double) map.get("lng");
        Double Py = (Double) map.get("lat");

        List<Assigment> list1 = new ArrayList<>();
        Integer val1=0,val2=0;
        List<Object> list2 = new ArrayList<>();
        for (Assigment assigment : list) {
            if (Distance.getDistance(Px, Py, assigment.getPlaceX(), assigment.getPlaceY()) < 2) {
                Map<String, Object> map1 = new HashMap<>();
                map1.put("lng", assigment.getPlaceX());
                map1.put("lat", assigment.getPlaceY());
                if(assigment.getAssigmentCondition()==2){
                    val2++;
                }else{
                    val1++;
                }
                list2.add(map1);
                list1.add(assigment);
            }
        }

        Map<String,Object> pieVal1=new HashMap<>();
        Map<String,Object> pieVal2=new HashMap<>();
pieVal2.put("name","紧急");pieVal2.put("value",val2);

        pieVal1.put("name","非紧急");pieVal1.put("value",val1);


        List<Object> list3=new ArrayList<>();
        list3.add(pieVal1);
        list3.add(pieVal2);


        Map<String, Object> mapVal = new HashMap<>();
        mapVal.put("assigmentList", list1);
        mapVal.put("points", list2);
        mapVal.put("pieVal", list3);
        returnData.setData(mapVal);
        returnData.setMsg("查询完毕");
        returnData.setStatus(true);


        return returnData;

    }

    @PostMapping("/getVal")
    public ReturnData getVal(@RequestBody Map<String, Object> map) {
        returnData.setData(null);

        //returnData.setData(assigmentService.getAllEmergency());

        returnData.setMsg("查询完毕");
        returnData.setStatus(true);
        return returnData;
    }


    @PostMapping("/Tuijian")
    public ReturnData Tuijian(@RequestBody Map<String, Object> map) {
        returnData.setData(null);

        returnData.setData(assigmentService.getTuiJian());

        returnData.setMsg("查询完毕");
        returnData.setStatus(true);
        return returnData;
    }




}
